from django.db import models

# Create your models here.
from superuser.models import SuperUser
from courses.models import Course
from utils.basemodel import BaseModel

# 订单状态
ORDER_STATUS_CHOICES = ((0, '待支付'), (1, '已支付'), (2, '已取消'), (3, '超时取消'))


class Order(BaseModel):
    user = models.ForeignKey(SuperUser, on_delete=models.CASCADE, verbose_name='用户')
    order_number = models.CharField(max_length=64, verbose_name='订单号')
    status = models.IntegerField(choices=ORDER_STATUS_CHOICES, default=0, verbose_name='订单状态')
    pay_type = models.IntegerField(choices=((0, '支付宝'), (1, '微信')), verbose_name='支付类型')
    total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总金额')
    pay_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='实付支付金额')
    pay_time = models.DateTimeField(verbose_name='支付时间', null=True, blank=True)
    pay_no = models.CharField(max_length=128, verbose_name='支付编号', null=True, blank=True)
    order_desc = models.CharField(max_length=128, verbose_name='订单备注', null=True, blank=True)
    user_coupon = models.BooleanField(default=False, verbose_name='是否使用优惠券')
    user_coupon_id = models.IntegerField(verbose_name='优惠券ID', null=True, blank=True)


    class Meta:
        db_table = 'mj_order'
        verbose_name = '订单'
        verbose_name_plural = verbose_name


class OrderDetail(BaseModel):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    order_number = models.CharField(max_length=64, verbose_name='订单号')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
    expire_time = models.DateTimeField(verbose_name='到期时间', null=True, blank=True)
    expire_text = models.CharField(max_length=64, verbose_name='过期时间文本提示')
    expire_day = models.IntegerField(verbose_name='有效期天数')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='购买价格')


    class Meta:
        db_table = 'mj_order_detail'
        verbose_name = '订单详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.order_number
